package 线程.demo16;

import org.omg.PortableInterceptor.InterceptorOperations;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/1 22:12
 */

public class BankCard {
    private  double changes;
    private boolean flag=false;
    public synchronized  void  save(double money){
            if (flag==true){
                try {
                    wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            changes=changes+money;
            System.out.println(Thread.currentThread().getName()+"存了"+money+"元，余额"+changes);
            flag=true;
            notify();
    }
    public synchronized  void  token(double money){
        if (flag==true){
            changes=changes-money;
            System.out.println(Thread.currentThread().getName()+"取了"+money+"元，余额"+changes);
            flag=false;
            notify();
        }
        try {
            wait();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
